home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / JDBCAdapter.java < prev    next >
Text File  |  1998-06-30  |  8KB  |  253 lines

  1. /*
  2.  * @(#)JDBCAdapter.java    1.6 98/02/10
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. /**
  22.  * An adaptor, transforming the JDBC interface to the TableModel interface.
  23.  *
  24.  * @version 1.20 09/25/97
  25.  * @author Philip Milne
  26.  */
  27.  
  28. import java.util.Vector;
  29. import java.sql.*;
  30. import com.sun.java.swing.table.AbstractTableModel;
  31. import com.sun.java.swing.event.TableModelEvent;
  32.  
  33. public class JDBCAdapter extends AbstractTableModel {
  34.     Connection          connection;
  35.     Statement           statement;
  36.     ResultSet           resultSet;
  37.     String[]            columnNames = {};
  38.     Class[]             columnTpyes = {};
  39.     Vector        rows = new Vector();
  40.     ResultSetMetaData   metaData;
  41.  
  42.     public JDBCAdapter(String url, String driverName,
  43.                        String user, String passwd) {
  44.         try {
  45.             Class.forName(driverName);
  46.             System.out.println("Opening db connection");
  47.  
  48.             connection = DriverManager.getConnection(url, user, passwd);
  49.             statement = connection.createStatement();
  50.         }
  51.         catch (ClassNotFoundException ex) {
  52.             System.err.println("Cannot find the database driver classes.");
  53.             System.err.println(ex);
  54.         }
  55.         catch (SQLException ex) {
  56.             System.err.println("Cannot connect to this database.");
  57.             System.err.println(ex);
  58.         }
  59.      }
  60.  
  61.     public void executeQuery(String query) {
  62.         if (connection == null || statement == null) {
  63.             System.err.println("There is no database to execute the query.");
  64.             return;
  65.         }
  66.         try {
  67.             resultSet = statement.executeQuery(query);
  68.             metaData = resultSet.getMetaData();
  69.  
  70.             int numberOfColumns =  metaData.getColumnCount();
  71.             columnNames = new String[numberOfColumns];
  72.             // Get the column names and cache them.
  73.             // Then we can close the connection.
  74.             for(int column = 0; column < numberOfColumns; column++) {
  75.                 columnNames[column] = metaData.getColumnLabel(column+1);
  76.             }
  77.  
  78.             // Get all rows.
  79.             rows = new Vector();
  80.             while (resultSet.next()) {
  81.                 Vector newRow = new Vector();
  82.                 for (int i = 1; i <= getColumnCount(); i++) {
  83.                 newRow.addElement(resultSet.getObject(i));
  84.                 }
  85.                 rows.addElement(newRow);
  86.             }
  87.             //  close(); Need to copy the metaData, bug in jdbc:odbc driver.
  88.             fireTableChanged(null); // Tell the listeners a new table has arrived.
  89.         }
  90.         catch (SQLException ex) {
  91.             System.err.println(ex);
  92.         }
  93.     }
  94.  
  95.     public void close() throws SQLException {
  96.         System.out.println("Closing db connection");
  97.         resultSet.close();
  98.         statement.close();
  99.         connection.close();
  100.     }
  101.  
  102.     protected void finalize() throws Throwable {
  103.         close();
  104.         super.finalize();
  105.     }
  106.  
  107.     //////////////////////////////////////////////////////////////////////////
  108.     //
  109.     //             Implementation of the TableModel Interface
  110.     //
  111.     //////////////////////////////////////////////////////////////////////////
  112.  
  113.     // MetaData
  114.  
  115.     public String getColumnName(int column) {
  116.         if (columnNames[column] != null) {
  117.             return columnNames[column];
  118.         } else {
  119.             return "";
  120.         }
  121.     }
  122.  
  123.     public Class getColumnClass(int column) {
  124.         int type;
  125.         try {
  126.             type = metaData.getColumnType(column+1);
  127.         }
  128.         catch (SQLException e) {
  129.             return super.getColumnClass(column);
  130.         }
  131.  
  132.         switch(type) {
  133.         case Types.CHAR:
  134.         case Types.VARCHAR:
  135.         case Types.LONGVARCHAR:
  136.             return String.class;
  137.  
  138.         case Types.BIT:
  139.             return Boolean.class;
  140.  
  141.         case Types.TINYINT:
  142.         case Types.SMALLINT:
  143.         case Types.INTEGER:
  144.             return Integer.class;
  145.  
  146.         case Types.BIGINT:
  147.             return Long.class;
  148.  
  149.         case Types.FLOAT:
  150.         case Types.DOUBLE:
  151.             return Double.class;
  152.  
  153.         case Types.DATE:
  154.             return java.sql.Date.class;
  155.  
  156.         default:
  157.             return Object.class;
  158.         }
  159.     }
  160.  
  161.     public boolean isCellEditable(int row, int column) {
  162.         try {
  163.             return metaData.isWritable(column+1);
  164.         }
  165.         catch (SQLException e) {
  166.             return false;
  167.         }
  168.     }
  169.  
  170.     public int getColumnCount() {
  171.         return columnNames.length;
  172.     }
  173.  
  174.     // Data methods
  175.  
  176.     public int getRowCount() {
  177.         return rows.size();
  178.     }
  179.  
  180.     public Object getValueAt(int aRow, int aColumn) {
  181.         Vector row = (Vector)rows.elementAt(aRow);
  182.         return row.elementAt(aColumn);
  183.     }
  184.  
  185.     public String dbRepresentation(int column, Object value) {
  186.         int type;
  187.  
  188.         if (value == null) {
  189.             return "null";
  190.         }
  191.  
  192.         try {
  193.             type = metaData.getColumnType(column+1);
  194.         }
  195.         catch (SQLException e) {
  196.             return value.toString();
  197.         }
  198.  
  199.         switch(type) {
  200.         case Types.INTEGER:
  201.         case Types.DOUBLE:
  202.         case Types.FLOAT:
  203.             return value.toString();
  204.         case Types.BIT:
  205.             return ((Boolean)value).booleanValue() ? "1" : "0";
  206.         case Types.DATE:
  207.             return value.toString(); // This will need some conversion.
  208.         default:
  209.             return "\""+value.toString()+"\"";
  210.         }
  211.  
  212.     }
  213.  
  214.     public void setValueAt(Object value, int row, int column) {
  215.         try {
  216.             String tableName = metaData.getTableName(column+1);
  217.             // Some of the drivers seem buggy, tableName should not be null.
  218.             if (tableName == null) {
  219.                 System.out.println("Table name returned null.");
  220.             }
  221.             String columnName = getColumnName(column);
  222.             String query =
  223.                 "update "+tableName+
  224.                 " set "+columnName+" = "+dbRepresentation(column, value)+
  225.                 " where ";
  226.             // We don't have a model of the schema so we don't know the
  227.             // primary keys or which columns to lock on. To demonstrate
  228.             // that editing is possible, we'll just lock on everything.
  229.             for(int col = 0; col<getColumnCount(); col++) {
  230.                 String colName = getColumnName(col);
  231.                 if (colName.equals("")) {
  232.                     continue;
  233.                 }
  234.                 if (col != 0) {
  235.                     query = query + " and ";
  236.                 }
  237.                 query = query + colName +" = "+
  238.                     dbRepresentation(col, getValueAt(row, col));
  239.             }
  240.             System.out.println(query);
  241.             System.out.println("Not sending update to database");
  242.             // statement.executeQuery(query);
  243.         }
  244.         catch (SQLException e) {
  245.             //     e.printStackTrace();
  246.             System.err.println("Update failed");
  247.         }
  248.         Vector dataRow = (Vector)rows.elementAt(row);
  249.         dataRow.setElementAt(value, column);
  250.  
  251.     }
  252. }
  253.